// miniprogram/pages/signAdd/signAdd.js
const db = wx.cloud.database()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        Height: 0,
        scale: 13,
        latitude: "",
        longitude: "",
        markers: [],
        controls: [{
                id: 1,
                iconPath: '/images/minus.png',
                position: {
                    left: 320,
                    top: 100 - 50,
                    width: 20,
                    height: 20
                },
                clickable: true
            },
            {
                id: 2,
                iconPath: '/images/add.png',
                position: {
                    left: 340,
                    top: 100 - 50,
                    width: 20,
                    height: 20
                },
                clickable: true
            }
        ],
        circles: [],
        name: '',
        number: ''
    },
    getName: function (e) {
        console.log(e)
        this.setData({
            name: e.detail.value
        })
    },
    getNumber: function (e) {
        console.log(e)
        this.setData({
            number: e.detail.value
        })
    },
    //立即签到
    signAdd: function () {
        //输出当前位置的经纬度
        console.log('经度', this.data.longitude)
        console.log('维度', this.data.latitude)

        //必须先填入姓名和学号
        if (this.data.number == '' || this.data.name == '') {
            wx.showModal({
                title: '请填写姓名和学号',
            })
            return;
        }

        // 打开地图选择位置 
        wx.chooseLocation({
            latitude: this.data.latitude,
            longitude: this.data.longitude,
            success: res => {
                console.log(res)
                //必须选择位置
                if (res.name == '' ) {
                    wx.showModal({
                        title: '请选择位置',
                    })
                    return;
                }
                //记录当前用户的打卡记录
                db.collection('signRecord').add({
                    data: {
                        latitude: res.latitude,
                        longitude: res.longitude,
                        name: this.data.name,
                        number: this.data.number,
                        addressName: res.name,
                        address: res.address,
                        nickName: wx.getStorageSync('user').nickName,
                        openid: wx.getStorageSync('user').openid,
                        imgUrl: wx.getStorageSync('user').imgUrl


                    }
                }).then(res => {
                    console.log("打卡成功", res)
                    wx.showToast({
                        title: '打卡成功！',
                        duration: 6000,
                        success: (res => {
                           this.setData({
                               number:'',
                               name:''
                           })
                        })
                    })

                }).catch(res => {
                    console.log("打卡失败", res)
                })

            }
        })
    },
    regionchange(e) {
        console.log("regionchange===" + e.type)
    },
    //点击merkers
    markertap(e) {
        console.log(e.markerId)

        wx.showActionSheet({
            itemList: ["A"],
            success: function (res) {
                console.log(res.tapIndex)
            },
            fail: function (res) {
                console.log(res.errMsg)
            }
        })
    },

    //点击缩放按钮动态请求数据
    controltap(e) {
        var that = this;
        console.log("scale===" + this.data.scale)
        if (e.controlId === 1) {
            // if (this.data.scale === 13) {
            that.setData({
                scale: --this.data.scale
            })
            // }
        } else {
            //  if (this.data.scale !== 13) {
            that.setData({
                scale: ++this.data.scale
            })
            // }
        }

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var _this = this;

        wx.getSystemInfo({
            success: function (res) {
                //设置map高度，根据当前设备宽高满屏显示
                _this.setData({
                    view: {
                        // Height: res.windowHeight
                        Height: 500
                    }

                })

            }
        })

        wx.getLocation({
            type: 'wgs84', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
            success: function (res) {

                _this.setData({
                    latitude: res.latitude,
                    longitude: res.longitude,
                    markers: [{
                        id: "1",
                        latitude: res.latitude,
                        longitude: res.longitude,
                        width: 50,
                        height: 50,
                        iconPath: "/images/map-filling.png",
                        title: "哪里"

                    }],
                    circles: [{
                        latitude: res.latitude,
                        longitude: res.longitude,
                        color: '#FF0000DD',
                        fillColor: '#7cb5ec88',
                        radius: 3000,
                        strokeWidth: 1
                    }]

                })
            }

        })

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})